package com.tsingteng.cosfun.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * 描述:只封装刷新的功能,下拉加载的功能用的是第三方的框架
 * 创建人: 肖利
 *时间: 2018/7/30/6/7.
 * qq: (872179966)
 */

public class MyRecycleView extends SmartRefreshLayout implements OnRefreshListener {
    private RecyclerView mRecyclerView;
    private boolean mIsRefreshing=false;
    /**
     * 刷新监听
     */
    private MyRecyclerViewListener mListener;


    public MyRecycleView(Context context) {
        this(context, null);
    }

    public MyRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRecyclerView = new RecyclerView(context);
        addView(mRecyclerView);
        //下拉加载不用这个框架
        setEnableLoadMore(false);
        initListener();
    }

    public void setMyRefreshListener(MyRecyclerViewListener myRecyclerViewListener) {
        this.mListener=myRecyclerViewListener;
    }
    public interface MyRecyclerViewListener {
        void onRefresh();
    }

    private void initListener() {
        mRecyclerView.setOnTouchListener(
                new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (mIsRefreshing) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }
        );
        addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                mRecyclerView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
                mRecyclerView.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
            }

            @Override
            public void onViewDetachedFromWindow(View v) {

            }
        });
        setOnRefreshListener(this);
    }
    public void setmIsRefreshing(boolean mIsRefreshing){
        this.mIsRefreshing = mIsRefreshing;
    }
    public boolean getIsRefreshing(){
        return  mIsRefreshing;
    }
    public void setLayoutManager(RecyclerView.LayoutManager manager) {
        mRecyclerView.setLayoutManager(manager);
    }
    public void setAdapter(RecyclerView.Adapter adapter) {
        mRecyclerView.setAdapter(adapter);
    }


    /**
     * 是否可以刷新,默认可以刷新
     * @param isCanRefresh
     */
    public void isCanRefresh(boolean isCanRefresh) {
        setEnableRefresh(isCanRefresh);
    }

    public void refreshComplete() {
        finishRefresh();

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        if (mListener != null) {
            mListener.onRefresh();
        }
    }


    /**
     * 增加滑动监听
     * @param listener
     */
    public void onScrollStateChanged(RecyclerView.OnScrollListener listener) {
        mRecyclerView.addOnScrollListener(listener);
    }


    public void srcollToPosition(int position) {
        mRecyclerView.scrollToPosition(position);
    }
}


